<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;

class ViewsInList
{
    private $entityName ="" ;
    private $item ="" ;
    private $itemName ="" ;
    private $itemType ="" ;
    private $itemView ="" ;

    public function getItem()
    {
        return $this->item;
    }
    public function getItemName()
    {
        return $this->itemName;
    }
    public function getItemType()
    {
        return $this->itemType;
    }
    public function getItemView()
    {
        return $this->itemView;
    }


    public function setItem($item,$entityName)
    {
        $this->item = $item;
        $this->entityName = $entityName;
        $this->setItemName($this->guessName($item));
        $this->setItemType($this->guessType($item));
        $this->setItemView($this->guessView($item));
        return $this;
    }
    public function setItemName($itemName)
    {
        $this->itemName = $itemName;
        return $this;
    }
    public function setItemType($itemType)
    {
        $this->itemType = $itemType;
        return $this;
    }
    public function setItemView($itemView)
    {
        $this->itemView = $itemView;
        return $this;
    }

    public function get()
    {
        return array(
            "entityName"    => $this->entityName,
            "item"          => $this->item,
            "itemName"      => $this->itemName,
            "itemType"      => $this->itemType,
            "itemView"      => $this->itemView,
        );
    }

    public function guessName($item)
    {
        $item = strtolower($item);
        $item = str_replace("_","",$item);
        switch ($item) {
            case "phone";       return "手机号";
            case "name";        return "名称";
            case "contact";     return "联系人";
            case "addtime";     return "创建时间";
            case "pic";         return "图片";
            case "level";       return "等级";
            case "parent";      return "上级";
            case "day";         return "日期";
            case "num";         return "数量";
            case "tips";        return "备注";
            case "status";      return "状态";
            case "intro";       return "介绍";
            case "lat";         return "纬度";
            case "lng";         return "经度";
            case "address";     return "地址";
            case "price";       return "价格";
            case "category";    return "分类";
            case "content";     return "内容";
        }
        return $item;
    }
    public function guessType($item)
    {
        if (preg_match("/((.+)?C|parent|category)/",$item) == 1) {//分类树
            return "select";
        }
        if (preg_match("/(.+)?Time/",$item) == 1) {//YY-mm-dd HH:ii:ss
            return "datetime";
        }
        if (preg_match("/(.+)?Date/",$item) == 1) {//YY-mm-dd
            return "date";
        }
        if (preg_match("/((.+)?Pic|pic)/",$item) == 1) {//图片
            return "pic";
        }
        if (preg_match("/((.+)?Detail|article)/",$item) == 1) {//富文本
            return "article";
        }
        if (preg_match("/is[A-Z](.+)?/",$item) == 1) {//0|1 是否
            return "switch";
        }
        if (preg_match("/(info|intro|message|msg|content)/",$item) == 1) {//多行文本
            return "textarea";
        }
        return "text";
    }
    public function guessView($item)
    {
        return $this->guessType($item);
    }

    public function createAddItem($conf)
    {
        $entityName = $conf["entityName"];
        $item = $conf["item"];
        $itemName = $conf["itemName"];
        $itemType = $conf["itemType"];
        switch ($itemType) {
            case "radio":
            case "checkbox":
            case "select":
            if (preg_match("/(.+)?Category/",$entityName) == 1) {//YY-mm-dd HH:ii:ss
                $isCat = "";
            }else{
                $isCat = "Category";
            }
        $str = '
        $'.$entityName.$isCat.'Model = new '.$entityName.$isCat.'Model($this->em);
        $'.$item.'Data = $'.$entityName.$isCat.'Model->getSons4Setting(0);
        array_unshift($'.$item.'Data,"根目录");
        $config["'.$item.'"] = array(
            "label"     =>"'.$itemName.'",
            "showType"  =>"'.$itemType.'",
            "dataType"  =>"",
            "class"     =>"",
            "option"    => ["other"=>["choices"=>$'.$item.'Data]]
        );';
                break;
            case "switch":
        $str = '
        $'.$item.'Data[0] = "否";
        $'.$item.'Data[1] = "是";
        $config["'.$item.'"] = array(
            "label"     =>"'.$itemName.'",
            "showType"  =>"radio",
            "dataType"  =>"",
            "class"     =>"",
            "option"    => ["other"=>["choices"=>$'.$item.'Data]]
        );';
                break;
            case "article":
            case "date":
            case "datetime":
            case "dateTime":
            case "text":
            case "textarea":
            case "pic":
        $str = '
        $config["'.$item.'"] = array(
            "label"     =>"'.$itemName.'",
            "showType"  =>"'.$itemType.'",
            "dataType"  =>"",
            "class"     =>"",
            "option"    => []
        );';
                break;
            default:
        $str = '
        $config["'.$item.'"] = array(
            "label"     =>"'.$itemName.'",
            "showType"  =>"text",
            "dataType"  =>"",
            "class"     =>"",
            "option"    => []
        );';
        }
        return $str;
    }

    public function createViewTitle($conf)
    {
        $itemName = $conf["itemName"];
        $str = '
        <th align="" width="">'.$itemName.'</th>';
        return $str;
    }

    public function createViewHeadAndTail($str)
    {
        return $str = '
        <th align="" width="">选择</th>'.$str.'
        <th align="" width="">操作</th>
    ';
    }

    public function createViewRow($conf,$entity)
    {
        $item = $conf["item"];
        $item = preg_replace("/([A-Z])/","_$1",$item);
        $itemView = $conf["itemView"];
        $macro = "text";
        switch ($itemView) {
            case "select":
            case "datetime":
            case "date":
                $macro = "text";
                break;
            case "pic":
                $macro = "pic";
                break;
            case "article":
                break;
            case "switch":
                $macro = "switch";
                break;
            case "textarea":
                $macro = "text";
                break;
            default:
                $macro = "text";
        }

        $str = "
            <td align='' width='' id='{$item}_{{ Entity.{$entity}_id }}'>
                {{ listItem.$macro('$item',Entity.{$entity}_$item,Entity.{$entity}_id) }}
            </td>";
        return $str;
    }

    public function createViewRowHeadAndTail($str,$entity)
    {
        return $str = "
            <td align='' width=''>
                <span class=\"checkall\"><input lay-skin=\"primary\" type=\"checkbox\" name=\"ids[]\" value=\"{{ Entity.{$entity}_id }}\"/></span>
            </td>" . $str . "
            <td align='' width=''>
                {{ listItem.manage('$entity','',Entity.{$entity}_id) }}
            </td>";
    }
}
